package com.ok.cartoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ok.cartoon.domain.po.OkCartoon;
import com.ok.cartoon.domain.po.OkCartoonCategory;
import com.ok.cartoon.mapper.OkCartoonCategoryMapper;
import com.ok.cartoon.service.IOkCartoonCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ok.cartoon.service.IOkCartoonService;
import com.ok.common.exception.CustomException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 漫画分类 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-28
 */
@Service
@RequiredArgsConstructor
public class OkCartoonCategoryServiceImpl extends ServiceImpl<OkCartoonCategoryMapper, OkCartoonCategory> implements IOkCartoonCategoryService {

    private final IOkCartoonService okCartoonService;

    @Override
    public void removeWith(Integer id) {
        LambdaQueryWrapper<OkCartoon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(OkCartoon::getCategoryId,id);
        //该分类下漫画的数量
        int count = okCartoonService.count(queryWrapper);

        if (count != 0){
            //如果该分类下关联漫画,则不能删除
            throw new CustomException("该分类下关联漫画,不能删除！");
        }
        this.removeById(id);
    }
}
